/**
 * 
 */
package aiproject3;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import aiproject3.agents.AgentFactory;
import aiproject3.agents.TrainingAgent;
import aiproject3.agents.AgentFactory.PredictorType;
import aiproject3.util.ArgParser;
import aiproject3.util.TestFileHelper;

/**
 * @author Chris
 */
public class Program {

	private static final String TRAINING_FILE = "groupTraining/GroupTrain_3.txt";
	private static final String TEST_FILE = "groupTesting/GroupTest_3.txt";
	
	private static int _correctGuesses = 0;
	private static int _incorrectGuesses = 0;
	
	/**
	 * Main entry point for the program
	 * @param args
	 */
	public static void main(String[] args) {
		File file = new File(TEST_FILE);
		TestFileHelper.CreateTestFile(TEST_FILE, 500);
		TestFileHelper.CreateTestFile(TRAINING_FILE, 500);
		
		// Get the type of model we want to use. Default is majority.
		ArgParser ap = new ArgParser(args);
		AgentFactory factory = new AgentFactory();
		TrainingAgent agent = factory.createAgent(AgentFactory.ModelType.Trigram, PredictorType.Majority, TRAINING_FILE);
		if(ap.argExists("model")) {
		    int idx = ap.argIdx("model");
		    int type = ap.parseArg("model", idx);
		    
		    switch(type) {
		    
		    case 2:
		        System.out.println("Using bigram modeling.");
		        agent = factory.createAgent(AgentFactory.ModelType.Bigram, PredictorType.NGramPredictor, TRAINING_FILE);
		        break;
		        
		    case 3:
                System.out.println("Using trigram modeling.");
                agent = factory.createAgent(AgentFactory.ModelType.Trigram, PredictorType.NGramPredictor, TRAINING_FILE);
                break;
		        
		    case 4:
		        System.out.println("Using Gaussian modeling.");
		        agent = factory.createAgent(AgentFactory.ModelType.Gaussian, PredictorType.DistributionPredictor, TRAINING_FILE);
		        break;
		        
		    case 5:
		        System.out.println("Using ANN modeling.");
		        agent = factory.createAgent(AgentFactory.ModelType.Unigram, PredictorType.ANNPredictor, TRAINING_FILE);
		        
		    default:
		        System.out.println("Using majority modeling.");
		        break;
		    }
		        
		}
		
		try {
			FileReader fr = new FileReader(file);
			int readOp;
			while ((readOp = fr.read()) != -1) {
				char c = (char)readOp;
				if (c == ' ' || c == ',') // Ignore spaces and commas
					continue;
				else {
				    char nxt = agent.predictNext();
					System.out.println("Prediction: " + nxt);//model.predictNext());
					System.out.println("Actual: " + c);
					if (((Character)nxt).equals(c))//model.predictNext()).equals(c))
						_correctGuesses++;
					else
						_incorrectGuesses++;
					agent.addToModel(c); // Update the model with new information
					System.out.println();
				}
			}
			fr.close();
			
			System.out.println("Predictive Accuracy: " + 
					((double)_correctGuesses / (_correctGuesses + _incorrectGuesses)) * 100.0 + "%");
			//System.out.println("Expected Accuracy: " + 
					//(((Integer)bigramAgent.getData().getValueFromModel((Character)bigramAgent.predictNext())) / (((double)_correctGuesses) + _incorrectGuesses) * 100.0) + "%");
					//(((Integer)model.getData().get((Character)model.predictNext())) / (((double)_correctGuesses) + _incorrectGuesses) * 100.0) + "%");
			System.out.println("Random Chance Accuracy: " + (1.0 / 26.0) * 100.0 + "%");
			System.out.println("Correct Guesses: " + _correctGuesses);
			System.out.println("Incorrect Guesses: " + _incorrectGuesses);
		} catch (FileNotFoundException ex) {
			System.out.println("Error: Unable to find file " + file.getPath());
		} catch (IOException ioex) {
			System.out.println("Error: Unable to read from file " + file.getPath());
		}

	}
}
